package com.kaifamiao.io.d0923;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;

public class FieldDemo {

    public static void main(String[] args) {
        Class<Student> studentClass = Student.class;

        try {
            Constructor<Student> constructor = studentClass.getDeclaredConstructor(String.class);

            Student student = constructor.newInstance("zhangsan");

            Field name = studentClass.getDeclaredField("name");

            if (name.getModifiers() == Modifier.PRIVATE){
                name.setAccessible(true);
            }
            System.out.println(name.get(student));

            name.set(student, "admin");

            System.out.println(name.getType());

            System.out.println(name.get(student));
            System.out.println(student);
//            Field staticField = studentClass.getField("staticField");
//
//            System.out.println(staticField);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
